package com.dier.webservices.restful.pdf;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.view.document.AbstractPdfView;

import com.dier.webservices.restful.model.Film;
import com.dier.webservices.restful.model.FilmList;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.draw.LineSeparator;


public class PdfView extends AbstractPdfView{

	@Override
	protected void buildPdfDocument(Map<String, Object> model,
			Document document, PdfWriter writer, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		Object dataFilm = null;
				
		dataFilm = model.get("miPelicula");
		
		if (dataFilm != null) {
			if (dataFilm instanceof FilmList) {
				FilmList listOfFilm = (FilmList) dataFilm;
				for (Film film : listOfFilm.getFilms()) {
					document.add( new Paragraph("Nombre:       " + film.getNombre()));
					document.add( new Paragraph("Duracion:     " + film.getDuracion().toString()));
					document.add( new Paragraph("Calificacion: " + film.getCalificacionFormateado()));
					document.add( new Paragraph("Genero:       " + film.getGeneroFormateado()));
					document.add( new Paragraph("F. Estreno:   " + film.getEstreno()));
					document.add( new Paragraph("Sipnosis: 	   " + film.getSipnosis()));
					document.add( new Paragraph(new Chunk(Chunk.NEWLINE)));
					document.add( new LineSeparator());
					document.add( new Paragraph(new Chunk(Chunk.NEWLINE)));
					
				}
			} else {
				Film film = (Film) model.get("miPelicula");
				document.add( new Paragraph("Nombre:       " + film.getNombre()));
				document.add( new Paragraph("Duracion:     " + film.getDuracion().toString()));
				document.add( new Paragraph("Calificacion: " + film.getCalificacionFormateado()));
				document.add( new Paragraph("Genero:       " + film.getGeneroFormateado()));
				document.add( new Paragraph("F. Estreno:   " + film.getEstreno()));
				document.add( new Paragraph("Sipnosis: 	   " + film.getSipnosis()));
			}
		} else {
			dataFilm =  model.get("isCorrect");
			document.add( new Paragraph("Operacion realizada con exito?  " + dataFilm.toString()));
		}
		
	}
}